package commonclass.dateandtime;





import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Lenl
 * @version v1.0
 * @create 2021-06-04 0:59
 * @description Calendar类的使用
 *
 * 抽象类：
 *  1.实例化创建子类
 *  2.调用静态方法getInstance()
 */
public class CalendarTest {

    @Test
    public void test(){
        Calendar calendar=Calendar.getInstance();

        //常用方法
        //get()
        int days= calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        int months=calendar.get(Calendar.WEEK_OF_MONTH);
        System.out.println(months);

        //set()
        calendar.set(Calendar.DAY_OF_MONTH,22);
        days=calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        //add()
        calendar.add(Calendar.DAY_OF_MONTH,3);//减为负数
        days=calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //getTime() 日历类--》Date
        Date date=calendar.getTime();
        System.out.println(date);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
        //setTime() Date-->日历类
        Date date1=new Date();
        calendar.setTime(date1);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        //注意：获取月份：1月为0，递增
        //     获取星期：周日为1，周六为7

    }


}
